<?php

namespace app\common\webmodel;


use app\common\lib\Util;
use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/6/4
 * Time: 2:25 下午
 */

/**
 * Class WebSixOneActive2021
 */
class WebSixOneActiveHistory2021 extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.web_six_one_active_2021_history';
    protected $cacheModel = 'web.six_active_history_2021';

    /**
     * 获取活动记录表
     * @param int $soId
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function acriveInfoHistory(int $soId)
    {
        $recordList = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $soId)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($soId)
            {
                return $this->getInfoByIdsInData($ids, [$soId], $sortType);
            }, 'id', [], 0);

        foreach ($recordList['data'] as $key => $item)
        {
            //名称匿名处理
            if ($item['name'] === '')
            {
                $recordList['data'][$key]['name'] = Util::hidtel($item['tel']);
            } else
            {
                $recordList['data'][$key]['name'] = Util::cutSubstr($item['name'], 5);
            }
            if (!$item['avatar'])
            {
                $recordList['data'][$key]['avatar'] = 'https://oss.goddessxzns.com/active/ac20210610/avatar.png';
            }
        }
        return $recordList['data'];
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where(['so_id' => $type[0]]);
            }

            $data = $this->order('id ' . $sortType)->column('tel,uid,so_id,score,name,avatar', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
